/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef COND_H_
#define COND_H_

#include <core/object.h>
#include <core/mutex.h>
#include <core/spinlock.h>
#include <core/task.h>

#include <stdlib/stdint.h>
#include <tools/list.h>

#define COND_WAKEUP_ONE            0x00
#define COND_WAKEUP_ALL            0x01

typedef struct {
    list_t  waitlist;
    spinlock_t  lock;
    object_t obj;
} DEFAULT_ALINMENT cond_t;

static inline void cond_get(cond_t *cond)
{
    object_get(&cond->obj);
}

static inline void cond_put(cond_t *cond)
{
    object_put(&cond->obj, offset_of(cond_t, obj), sizeof(cond_t));
}

int  cond_init(cond_t *cond);
int cond_timewait(cond_t *cond, mutex_t *mutex, int usec);
int  cond_signal(cond_t *cond);
int cond_broadcast(cond_t * cond);
void cond_runlist_clear(tcb_t* task);
int cond_timeout_mutex_lock(tcb_t *task);
int cond_destroy(cond_t* cond);

#define LOCK_COND(cond)   spin_lock(&(cond->lock));
#define UNLOCK_COND(cond) spin_unlock(&(cond->lock));

#endif
